package com.ntt.benew.dictionary.model


import com.ntt.benew.dictionary.NttDictionaryManager
import com.ntt.benew.dictionary.api.YDDictManager
import com.ntt.benew.dictionary.listener.IAnalysisListener
import com.ntt.benew.dictionary.utils.HttpUtil
import com.ntt.core.nlogger.NLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class AnalysisModel {

    companion object {
        const val GPT_STRUCTURE_URL =
            "https://gw.benewtech.cn/ai-assistant/english/sentence/structure"

    }


    /**
     * 获取句子解析内容
     */
    fun postAnalysisSentenceStructure(data: String, callback: IAnalysisListener?) {
        CoroutineScope(Dispatchers.IO).launch {
//            val contentBody = HashMap<String, String>().apply {
//                put("\"content\"", "\"Chip and Wilf were tracking Dad.\"")
//            }
            val contentBody = "{\"content\":\"$data\"}"
            //组装请求头
            val head = mutableMapOf(
                "Content-Type" to "application/json;charset=UTF-8",
                "Authorization" to "Bearer ${NttDictionaryManager.authToken}",
                "deviceId" to NttDictionaryManager.deviceId,
                "Accept" to "application/json",
                "Authorization" to "Bearer ${NttDictionaryManager.authToken}",
                "User-Agent" to HttpUtil.USER_AGENT + NttDictionaryManager.deviceVersion,
                "X-Device-User-Agent" to HttpUtil.USER_AGENT + NttDictionaryManager.deviceVersion,
            )
            NLogger.d("开始请求GPT ${NttDictionaryManager.authToken} , $contentBody")
            val d: String? = HttpUtil.doPost1(
                GPT_STRUCTURE_URL,
                head,
                contentBody
            )
            launch(Dispatchers.Main) {
                callback?.onSuccess(d)
            }
        }

    }

    /**
     * 有道少儿词典查词 查单个单词
     */
    fun postQueryDictWord(word: String, langType: String, callback: IAnalysisListener?) {
        CoroutineScope(Dispatchers.IO).launch {
            val data = YDDictManager.queryDictWord(word, langType)
            launch(Dispatchers.Main) {
                callback?.onSuccess(data)
            }
        }
    }


//
//
//    /**
//     * 获取练习题
//     */
//    fun getExercise(uid: Long, observer: BaseObserver<ExerciseEntity>) {
//        HTTPManager.getInstance().obtainService(TranslateService::class.java)
//            .getExercises(uid)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(observer)
//    }
//
//
//    /**
//     * 收藏句子
//     */
//    fun postFavoriteSentence(data: HashMap<String, Any>, observer: BaseObserver<Any>) {
//        HTTPManager.getInstance().obtainService(TranslateService::class.java)
//            .postFavoriteSentence(data)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(observer)
//    }
//
//
//    /**
//     * 获取收藏的句子
//     */
//    fun getFavoriteSentence(text: String, observer: BaseObserver<Any>) {
//        HTTPManager.getInstance().obtainService(TranslateService::class.java)
//            .getFavoriteSentence(text)
//            .subscribeOn(Schedulers.io())
//            .observeOn(AndroidSchedulers.mainThread())
//            .subscribe(observer)
//    }
//
//
//    /**
//     * 错误上报
//     */
//    fun postErrorReport(uid: Long, observer: BaseObserver<Any>) {
//        val params = ReportErrorBody(uid, "AI_SENTENCE")
//        HTTPManager.getInstance().obtainService(TranslateService::class.java)
//            .postErrorReport(params)
//            .subscribeOn(Schedulers.io())
//            .observeOn(Schedulers.io())
//            .subscribe(observer)
//    }
}